﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Royalsoft.Common;

namespace NJL.WebUsers.Shop
{
    public partial class ShopShare : BasePageVisitor
    {
        NJL.BLL.NJL_CS_NEWS_TYPE bllNewsType = new BLL.NJL_CS_NEWS_TYPE();
        NJL.BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
        NJL.Model.NJL_NEWS modelNews = new Model.NJL_NEWS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UserId == "" || UserName == "")
                {
                    Response.Redirect("/Login.aspx?url=" + Request.RawUrl, true);
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            modelNews.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            if (FileUpload1.HasFile)
            {
                modelNews.IMAGE_CODE = SaveUploadFile(Request.Files);
            }
            modelNews.NJL_PERSON_ID = UserId;
            modelNews.TYPE_CODE = "101";
            modelNews.NJL_SYS_BASE_ID = WebSiteCode;
            modelNews.NEWS_TITLE = PageValidate.InputText(txtAttractionsTitle.Text.Trim(), 50);
            modelNews.NEWS_SUBJECT_TITLE = PageValidate.InputText(txtAttractionFtitle.Text.Trim(), 50);
            modelNews.NEWS_TAG = PageValidate.InputText(txtTag.Text.Trim(), 100);
            modelNews.NEWS_CONTECT = txtContent.Text.Trim();
            modelNews.CREATE_USER_NAME = UserName;
            modelNews.CREATE_USER_ID = UserId;
            modelNews.CREATE_DATE_TIME = DateTime.Now;
            modelNews.SHARE_COMPANY_ID = Request.QueryString["id"];
            if (bllNews.Add(modelNews))
            {
                string message;
                if (HandleIntegral(UserId, "2", out message))
                {

                }           
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "4";  //游记4
                            modelTag.USED_COUNT = 0;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion
                Page.ClientScript.RegisterStartupScript(GetType(), "closediv", "<script>window.top.location='" + Request.QueryString["url"] + "'</script>");
            }
        }
    }
}